﻿using App_1.Contracts;
using App_1.Data.Repositoriescs.Extensions;
using App_1.Entites;
using App_1.RequestFeatures;
using App_1.ResponseType.Paging;
using Microsoft.EntityFrameworkCore;

namespace App_1.Data.Repositoriescs
{
    public class PlayerRepository : RepositoryBase<Player>, IPlayerRepository
    {
        public PlayerRepository(GameManagementDbContext repositoryContext) : base(repositoryContext)
        {
        }

        public Task<List<Player>> GetAllPlayers()
        {
            return FindAll()
                .OrderBy(player => player.Id)
                .ToListAsync();
        }

        public Player? GetPlayerById(Guid playerId)
        {
            return FindByCondition(player => player.Id == playerId)
                .FirstOrDefault();
        }

        public Player? GetPlayerWithCharacters(Guid playerId)
        {
            return FindByCondition(player => player.Id == playerId)
                .Include(player => player.characters)
                .FirstOrDefault();
        }

        public void CreatePlayer(Player player)
        {
            Create(player);
        }

        public void UpdatePlayer(Player player)
        {
            Update(player);
        }

        public void DeletePlayer(Player player)
        {
            Delete(player);
        }

        public PagedList<Player> GetPlayers(PlayerParameter parameter)
        {
            return FindByCondition(player => player.DateCreated.Date >= parameter.MinDateCreated.Date
                                             && player.DateCreated.Date <= parameter.MaxDateCreated.Date)
                .SearchByAccount(parameter.Account)
                .OrderByQuery(parameter.OrderBy)
                .ToPagedList(parameter.PageNumber, parameter.PageSize);
        }
    }
}
